package jdbc;

import java.sql.*;
import java.util.Iterator;
import java.util.*;

/**
 * Created by luozhen on 2016/8/17.
 */
public class HashMapDemo{
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        Connection conn = getConnection("root", "110625");
        HashMap map = new HashMap<>();

        for (int i=0;i<100;i++) {
            map.put(i, "hello");

        }
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            String name = (String)map.get(key);
            System.out.println(key+name);
            releaseConnection(conn);
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime + "ms");

        long startTime1 = System.currentTimeMillis();
        HashMap hashmap1 = new HashMap();
        for (int i = 0; i < 100; i++ ) {
            hashmap1.put(i, "thanks");
            }
        Map map1 = new HashMap();
        Iterator iterator1 = map.entrySet().iterator();
        while (iterator1.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator1.next();
            System.out.println(entry.getValue());
            }
        long endTime1 = System.currentTimeMillis();
        System.out.println(endTime1 - startTime1 + "ms");
    }

    //1.查询数据，定义的query方法
    public static void query(Connection conn){
        String Sql="select * from employees";
        try{
            Statement stmt=conn.createStatement(); //也可以使用PreparedStatement来做
            ResultSet rs=stmt.executeQuery(Sql);//执行sql语句并返还结束

            while(rs.next()){//遍历结果集
                System.out.println("人员编号:"+rs.getString("employee_id")+"工资:"+rs.getString("salary"));
            }
            if(rs !=null){
                try{
                    rs.close();
                } catch (SQLException e){
                    e.printStackTrace();
                }
            }
            if(stmt !=null){
                try{
                    stmt.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(conn !=null){
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /** 数据库连接*/
    public static Connection getConnection(String user, String pass) {

        Connection conn = null;//声明连接对象
        String driver = "com.mysql.jdbc.Driver";// 驱动程序类名
        String url = "jdbc:mysql://localhost:3306/db_book?" // 数据库URL
                + "useUnicode=true&characterEncoding=UTF8";// 防止乱码
        try {
            Class.forName(driver);// 注册(加载)驱动程序
            conn = DriverManager.getConnection(url, user, pass);// 获取数据库连接
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**释放数据库连接*/
    public static void releaseConnection(Connection conn) {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
